$(function () {
    var indentstate = $("#indentstate").val();
    if (isempty(indentstate))
        topage(0, 0);
    else if (indentstate == 0)
        one();
    else if (indentstate == 1)
        two();
    else if (indentstate == 2)
        three();
    else if (indentstate == 3)
        four();
});

function topage(page, state) {
    var token = $("#token").val();
    if (isempty(token)) {
        location.href = "/except/login.jsp";
        return;
    }
    var jsonstr = "{token:" + token + "}";
    var url = "/except/indentcontroller/indentlist.action";
    $.ajax({
        url: url,
        type: 'post',
        dataType: "json",
        data: {
            json: jsonstr,
            page: page,
            indentstate: state
        },
        async: false,
        success: function (data) {
            if (page == 0)
                $("#load_more_page").empty();
            var code = data.code;
            var message = data.message;
            if (code == 10001 || code == 10002)
                location.href = "/except/login.jsp";
            else if (code == 20000) {
                var list = "";
                var datas = data.data;
                var indentshopinfo = "";
                $(datas).each(function () {
                    indentshopinfo += '<span onclick="location.href=\'/except/files/indent/orderinfo.jsp?indentnum=' + this.indent_num + '&state=' + state + '\'">' +
                        '<span>' +
                        '<div class="store_name">' +
                        '<div style="float: left;">' +
                        '<img src="img/logo_icon.png" class="add_img"/>' +
                        '<span>摘记良品</span>' +
                        '</div>' +
                        '<div style="float: right;">' +
                        '<span class="right_img">' + this.indentstatedesc + '</span>' +
                        '</div>' +
                        '</div>' +
                        '<hr style="height: 1px; width: 100%; background-color: #f4f4f4; border: none;"/>' +
                        '</span>';
                    $(this.indentshop).each(function () {
                        indentshopinfo += '<div class="goods">' +
                            //  页面加载数据的图片展示
                            // '<img src="' + this.img + '" class="item_img" onerror="imgerror(this,\'img/onerror.png\');"/>' +
                                '<div style="float: left;margin-right: 10px; width:85px; height:85px;overflow-y: hidden; ' +
                                'background: url('+this.img+') no-repeat center;background-size: cover;"' +
                                ' onerror="imgerror(this,\'img/onerror.png\');"></div>'+
                            '<ul class="item_ul">' +
                            '<li><span class="item_name">' + this.shop_name + '</span></li>' +
                            '<li>' +
                            '<div style="margin-top: 10px;">' +
                            '<span class="item_desc">' + this.name + '</span>' +
                            '<span class="item_buy_num">x1</span>' +
                            '</div>' +
                            '</li>' +
                            '<li><span class="item_money">￥: <i style="font-size: 16px;">' + this.price + '</i></span></li>' +
                            '</ul>' +
                            '</div>';
                    });
                    indentshopinfo += '<hr style="height: 1px; width: 97%; float: right; background-color: #f4f4f4; border: none;"/>' +
                        '<div class="trans_div">' +
                        '<span class="tran_span">(含运费' + this.freight_money + '元)</span>' +
                        '<span class="tran_money">合计￥<i class="tran_money">' + this.paymoney + '</i></span>' +
                        '</div>' +
                        '<hr style="height: 1px; width: 97%; float: right; background-color: #f4f4f4; border: none;"/>' +
                        '</span>' +
                        '<div class="trans_div">' +
                        '<div class="div_two">';
                    if (this.indent_state == 1) {
                        indentshopinfo += '<span class="left" onclick="indent_handle(1,\'' + this.indent_num + '\');">取消订单</span>' +
                            '<span class="right" onclick="indent_handle(2,\'' + this.indent_num + '\');">立即付款</span>';
                    } else if (this.indent_state == 2) {
                        indentshopinfo += '<span class="left" onclick="indent_handle(3,\'' + this.indent_num + '\');">联系客服</span>' +
                            '<span class="right" onclick="indent_handle(4,\'' + this.indent_num + '\');">提醒发货</span>';
                    } else if (this.indent_state == 3) {
                        indentshopinfo += '<span class="left"  onclick="indent_handle(5,\'' + this.indent_num + '\');">查看物流</span>' +
                            '<span class="right" onclick="indent_handle(6,\'' + this.indent_num + '\');">确认收货</span>';
                    } else if (this.indent_state == 4 || this.indent_state == 5) {
                        indentshopinfo += '<span class="right" onclick="indent_handle(7,\'' + this.indent_num + '\');">删除</span>';
                    } else {

                    }
                    indentshopinfo += '</div>' +
                        '</div>' +
                        '</div>' +
                        '';
                });
                $("#load_more_page").append(indentshopinfo);
                remove_advertising();
            } else if (code == 20013) {
                if (page == 0){
                    $("#not_data").show();
                }
            } else {
                $("#hint_message").html(message);
                $("#hintbg").show();
            }
            $("#page").val(page * 1 + 1);
            $("#indentstate").val(state);
        }
    });

}

function indent_handle(state, indentnum) {
    var url = "";
    if (state == 1) {
        url = "/except/indentcontroller/cancleindent.action";
    } else if (state == 2) {
        $("#pay_div").addClass("pop_payss");
        $("#pay_bg").show();
        payindent(indentnum);
        return;
    } else if (state == 3) {
        $("#hint_message").html("开发中，敬请期待");
        $("#hintbg").show();
        return;
    } else if (state == 4) {
        url = "/except/indentcontroller/sendadvice.action";
    } else if (state == 5) {
        location.href = "/except/files/indent/logistic.jsp?indentnum="+indentnum;
        return;
    } else if (state == 6) {
        url = "/except/indentcontroller/confirmindent.action";
    } else if (state == 7) {
        url = "/except/indentcontroller/delindent.action";
    }
    var token = $("#token").val();
    if (isempty(token)) {
        location.href = "/except/login.jsp";
        return;
    }
    var jsonstr = "{token:" + token + "}";
    $.ajax({
        url: url,
        type: 'post',
        dataType: "json",
        data: {
            json: jsonstr,
            indentnum: indentnum
        },
        async: false,
        success: function (data) {
            var code = data.code;
            var message = data.message;
            if (code == 10001 || code == 10002)
                location.href = "/except/login.jsp";
            else if (code == 20000) {
                $("#hint_message").html("操作成功");
                $("#hintbg").show();
                var indentstate = $("#indentstate").val();
                topage(0, indentstate);
            } else {
                $("#hint_message").html(message);
                $("#hintbg").show();
            }
        },
        error: function (data) {
            $("#hint_message").html("网络异常");
            $("#hintbg").show();
        }
    });
}

function bind() {
    var indentstate = $("#indentstate").val();
    topage(0, indentstate);
}

function showPopup() {
    $("#pay_div").removeClass("pop_payss");
    $("#pay_bg").hide();
}

function payindent(indentnum) {
    var url = "/except/indentcontroller/indentinfo.action";
    var token = $("#token").val();
    $("#indentnum").val(indentnum);
    if (isempty(token)) {
        location.href = "/except/login.jsp";
        return;
    }
    var jsonstr = "{token:" + token + "}";
    $.ajax({
        url: url,
        type: 'post',
        dataType: "json",
        data: {
            json: jsonstr,
            indentnum: indentnum
        },
        async: false,
        success: function (data) {
            var code = data.code;
            var message = data.message;
            if (code == 10001 || code == 10002)
                location.href = "/except/login.jsp";
            else if (code == 20000) {
                var sparemoney = data.data.sparemoney;
                var paymoney = data.data.paymoney;
                $("#sparemoney").html(sparemoney);
                $("#money").html(paymoney);
                if (sparemoney * 1 > paymoney * 1) {
                    $("#spare_pay").attr("checked", true);
                    $("#wechat_pay").attr("checked", false);
                } else {
                    $("#spare_pay").attr("checked", false).attr("disabled", true);
                    $("#wechat_pay").attr("checked", true);
                }
            } else {
                $("#hint_message").html(message);
                $("#hintbg").show();
            }
        },
        error: function (data) {
            $("#hint_message").html("网络异常");
            $("#hintbg").show();
        }
    });
}

function pay_indent() {
    var token = $("#token").val();
    if (isempty(token)) {
        location.href = "/except/login.jsp";
        return;
    }
    var paytype = 1
    $("#paytype :radio").each(function () {
        var checked = $(this).attr("checked");
        if (checked)
            paytype = $(this).val();
    });
    var indentnum = $("#indentnum").val();
    var jsonstr = "{token:" + token + "}";
    var url = "/except/indentcontroller/payindent.action";
    $.ajax({
        url: url,
        type: 'post',
        dataType: "json",
        data: {
            json: jsonstr,
            indentnum: indentnum,
            paytype: paytype
        },
        async: false,
        success: function (data) {
            var code = data.code;
            var message = data.message;
            if (code == 10001 || code == 10002)
                location.href = "/except/login.jsp";
            else if (code == 20000) {
                $("#nonce_str").val(data.data.nonce_str);
                $("#result_sign").val(data.data.result_sign);
                $("#timestamp").val(data.data.indenttime);
                $("#prepay_id").val(data.data.prepay_id);
                $("#payinfo").submit();
            } else {
                $("#hint_message").html(message);
                $("#hintbg").show();
            }
        }
    });
}

$(window).scroll(function () {
    var indentstate = $("#indentstate").val();
    //$(document).scrollTop() 获取垂直滚动的距离
    //$(document).scrollLeft() 这是获取水平滚动条的距离
    if ($(document).scrollTop() <= 0) {
        topage(0, indentstate);
    }
    if ($(document).scrollTop() >= $(document).height() - $(window).height()) {
        var page = $("#page").val();
        topage(page, indentstate);
    }
});

/*加载全部*/
function one() {
    $("#span_one").attr("class", "span_red");
    $("#span_two").attr("class", "span_gray");
    $("#span_three").attr("class", "span_gray");
    $("#span_four").attr("class", "span_gray");
    $("#hr_one").attr("class", "hr_red");
    $("#hr_two").attr("class", "hr_white");
    $("#hr_three").attr("class", "hr_white");
    $("#hr_four").attr("class", "hr_white");
    $("#not_data").hide();
    $("#indentstate").val(0);
    topage(0, 0);
}
/*加载待付款*/
function two() {
    $("#span_one").attr("class", "span_gray");
    $("#span_two").attr("class", "span_red");
    $("#span_three").attr("class", "span_gray");
    $("#span_four").attr("class", "span_gray");
    $("#hr_one").attr("class", "hr_white");
    $("#hr_two").attr("class", "hr_red");
    $("#hr_three").attr("class", "hr_white");
    $("#hr_four").attr("class", "hr_white");
    $("#not_data").hide();
    $("#indentstate").val(1);
    topage(0, 1);
}
/*加载代发货的*/
function three() {
    $("#span_one").attr("class", "span_gray");
    $("#span_two").attr("class", "span_gray");
    $("#span_three").attr("class", "span_red");
    $("#span_four").attr("class", "span_gray");
    $("#hr_one").attr("class", "hr_white");
    $("#hr_two").attr("class", "hr_white");
    $("#hr_three").attr("class", "hr_red");
    $("#hr_four").attr("class", "hr_white");
    $("#not_data").hide();
    $("#indentstate").val(2);
    topage(0, 2);
}
/*加载待收货的*/
function four() {
    $("#span_one").attr("class", "span_gray");
    $("#span_two").attr("class", "span_gray");
    $("#span_three").attr("class", "span_gray");
    $("#span_four").attr("class", "span_red");
    $("#hr_one").attr("class", "hr_white");
    $("#hr_two").attr("class", "hr_white");
    $("#hr_three").attr("class", "hr_white");
    $("#hr_four").attr("class", "hr_red");
    $("#not_data").hide();
    $("#indentstate").val(3);
    topage(0, 3);
}
